package com.hatic.process;

import java.util.Map;

import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;
import org.jxstar.util.DateUtil;
import org.jxstar.util.key.KeyCreator;

public class ProjectProcessBO extends BusinessObject{
	
	private static final long serialVersionUID = 1L;

	/**
	 * 选择项目生产项目进度计划
	 * @param projectids
	 * @param username
	 * @param userid
	 * @return
	 */
	public String imProject(String[] projectids,String username ,String userid){
		
		if(projectids == null || projectids.length == 0){
			_log.showDebug("没有选择");
			return _returnFaild;
		}
		for(int i =0;i<projectids.length;i++){
			if(!createProjectProcess(projectids[i], username , userid)){
				_log.showDebug("导入失败");
				return _returnFaild;
			}
		}
		
		return _returnSuccess;
	}
	
	/**
	 * 选择项目进度计划，生产新的计划做计划调整
	 * @param processids
	 * @param username
	 * @param userid
	 * @return
	 */
	public String newProcess(String[] processids,String username ,String userid){
		
		if(processids == null || processids.length == 0){
			_log.showDebug("没有选择进度计划");
			return _returnFaild;
		}
		for(int i =0;i<processids.length;i++){
			if(!createNewProcess(processids[i], username , userid)){
				_log.showDebug("导入失败");
				return _returnFaild;
			}
		}
		
		return _returnSuccess;
	}

	
	private boolean createNewProcess(String processId, String username, String userid) {
		String querySQL="select * from project_process where process_id = ?";
		_log.showDebug("==============processId='"+processId+"';sql="+querySQL);
		
		DaoParam qparam = _dao.createParam(querySQL);
		qparam.addStringValue(processId);
		Map<String, String> qmap=_dao.queryMap(qparam);
		
		String insertSQL="insert into project_process(process_id,project_id,project_name,dept_name,dept_id,"+
		"check_fileno,money_type,auth_type,"+
		"plan_money,project_user,project_userid ,proecss_user,proecss_userid,"+
		"proecss_date ,add_userid,add_date,project_code,auditing,process_state,is_turn,"+
		"date_projectlz,date_ztb,date_contract,date_projectend,date_check,date_moneycheck,date_checkend,memo,process_oldid,check_dept) "+
		"values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,"+
		"?,?,?,?,?,?,?,?,?,?)";
		
		String processId1=KeyCreator.getInstance().createKey("process_id");
		_log.showDebug("==============sql="+insertSQL);
		
		DaoParam cparam = _dao.createParam(insertSQL);
		cparam.addStringValue(processId1);
		cparam.addStringValue(qmap.get("project_id"));
		cparam.addStringValue(qmap.get("project_name"));
		cparam.addStringValue(qmap.get("dept_name"));
		cparam.addStringValue(qmap.get("dept_id"));
		cparam.addStringValue(qmap.get("check_fileno"));
		cparam.addStringValue(qmap.get("money_type"));
		cparam.addStringValue(qmap.get("auth_type"));
		cparam.addDoubleValue(qmap.get("plan_money"));
		cparam.addStringValue(qmap.get("project_user"));
		cparam.addStringValue(qmap.get("project_userid"));
		cparam.addStringValue(username);
		cparam.addStringValue(userid);
		cparam.addDateValue(DateUtil.getToday());
		cparam.addStringValue(userid);
		cparam.addDateValue(DateUtil.getToday());
		cparam.addStringValue(qmap.get("project_code"));
		cparam.addStringValue("0");
		cparam.addStringValue("0");
		cparam.addStringValue("1");
		cparam.addDateValue(qmap.get("date_projectlz"));
		cparam.addDateValue(qmap.get("date_ztb"));
		cparam.addDateValue(qmap.get("date_contract"));
		cparam.addDateValue(qmap.get("date_projectend"));
		cparam.addDateValue(qmap.get("date_check"));
		cparam.addDateValue(qmap.get("date_moneycheck"));
		cparam.addDateValue(qmap.get("date_checkend"));
		cparam.addStringValue(qmap.get("memo"));
		cparam.addStringValue(qmap.get("process_id"));
		cparam.addStringValue(qmap.get("check_dept"));
				
		return _dao.update(cparam);
}

	private boolean createProjectProcess(String projectId,String username ,String userid) {
		String querySQL="select * from project_base where project_id = ?";
		_log.showDebug("==============projectId='"+projectId+"';sql="+querySQL);
		
		DaoParam qparam = _dao.createParam(querySQL);
		qparam.addStringValue(projectId);
		Map<String, String> qmap=_dao.queryMap(qparam);
		
		String insertSQL="insert into project_process(process_id,project_id,project_name,dept_name,dept_id,"+
		"check_fileno,money_type,auth_type,"+
		"plan_money,project_user,project_userid ,proecss_user,proecss_userid,"+
		"proecss_date ,add_userid,add_date,project_code,auditing,process_state,is_turn,check_dept) "+
		"values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		
		String processId=KeyCreator.getInstance().createKey("process_id");
		_log.showDebug("==============sql="+insertSQL);
		
		DaoParam cparam = _dao.createParam(insertSQL);
		cparam.addStringValue(processId);
		cparam.addStringValue(qmap.get("project_id"));
		cparam.addStringValue(qmap.get("project_name"));
		cparam.addStringValue(qmap.get("dept_name"));
		cparam.addStringValue(qmap.get("dept_id"));
		cparam.addStringValue(qmap.get("check_fileno"));
		cparam.addStringValue(qmap.get("money_type"));
		cparam.addStringValue(qmap.get("auth_type"));
		cparam.addDoubleValue(qmap.get("plan_money"));
		cparam.addStringValue(qmap.get("project_user"));
		cparam.addStringValue(qmap.get("project_userid"));
		cparam.addStringValue(username);
		cparam.addStringValue(userid);
		cparam.addDateValue(DateUtil.getToday());
		cparam.addStringValue(userid);
		cparam.addDateValue(DateUtil.getToday());
		cparam.addStringValue(qmap.get("project_code"));
		cparam.addStringValue("0");
		cparam.addStringValue("0");
		cparam.addStringValue("0");
		cparam.addStringValue(qmap.get("check_dept"));
		
		return _dao.update(cparam);
	}
}
	
		


